package com.tryLogin.controller;
import java.io.Console;
import java.util.List;
import com.tryLogin.Service.UsersService;
import com.tryLogin.common.AESUtil;
import com.tryLogin.common.AjaxResult;
import com.tryLogin.common.Base64Util;
import com.tryLogin.common.StringUtils;
import com.tryLogin.domain.Users;
import com.tryLogin.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.awt.*;
//前后端分离者也
//前后端分离简化了参数，
@RestController
@Controller
@RequestMapping("/users")
@CrossOrigin
public class UserController {

        @Autowired
        private UsersService usersService;


    /**
     *登录是也
     */
    AESUtil aesUtil=new AESUtil();
    @RequestMapping(value = "/login")
    public AjaxResult  login(@RequestBody Users users) throws Exception {

//         String tel=  aesUtil.decrypt(users.getTelNum());//解密
//        System.out.println(tel);
//        Users u=new Users();
//        u.setTelNum(tel);
        List<Users> userss  =usersService.checkByTel(users);

        if(StringUtils.isEmpty(userss)){
           return new AjaxResult(00002,"密码错误或不存在此用户");
        }
           return new AjaxResult(000000,"登录成功");
    }
    /**
     * 获取客户信息列表
     */
    @RequestMapping(value = "/checkAllUsers")
        public AjaxResult checkAllUsers() {
         List<Users>usersList= usersService.checkAll();
        return AjaxResult.success(usersList);
    }

    /**
     * 添加客户信息
     */
    @RequestMapping(value = "/insertUsers")
    public AjaxResult insertUsers(@RequestBody Users u) {
//        /*
//          进行加密操作
//         */
//       String myIdCard= Base64Util.encryptBASE64(u.getIdCard().getBytes());
//        String myName= Base64Util.encryptBASE64(u.getName().getBytes());
//        String myTel=  Base64Util.encryptBASE64(u.getTelNum().getBytes());
//        System.out.println(myName);
//        System.out.println(myIdCard);
//        System.out.println(myTel);
//        /*
//          解密,重新赋值
//         */
//       u.setIdCard(Base64Util.decryptBASE64(myIdCard).toString());
//        u.setName(Base64Util.decryptBASE64(myName).toString());
//        u.setTelNum(Base64Util.decryptBASE64(myTel).toString());
//        if(judge(u,session)==true){
//            int i= usersService.add(u);
//            return AjaxResult.success("添加成功");
//        }else{
//            return AjaxResult.error("添加失败");
//        }
        int i= usersService.add(u);
        if(i>0){
            return new AjaxResult(000000,"添加成功");
        }else{
            return new AjaxResult(100000,"添加失败");
        }

    }


//    /*
//      校验Session信息和数据库是否一致
//      */
//    private boolean judge(Users user,HttpSession session){
//        Users one=usersService.checkOne(user);
//        if(one.getIdCard().equals((String) session.getAttribute("idCard")) &&
//                one.getName().equals((String) session.getAttribute("name")) &&
//                one.getTelNum().equals((String) session.getAttribute("name"))
//        ){
//            return true;
//
//        }else{
//            return false;
//        }
//    }

    @RequestMapping(value = "/updateUsers")
    public AjaxResult updateUsers(@RequestBody Users u) {
       int i=  usersService.update(u);
        if(i>0){
            return new AjaxResult(000000,"修改成功");
        }else{
            return new AjaxResult(000002,"修改失败");
        }
   }

    @RequestMapping(value = "/deleteUsers")
    public AjaxResult deleteUsers(@RequestBody Users u) {
        int i=  usersService.delete(u);
        if(i>0){
            return new AjaxResult(000000,"删除成功");
        }else{
            return new AjaxResult(000002,"删除失败");
        }
    }

    @RequestMapping(value = "/checkOne")
    public AjaxResult checkOne(@RequestBody Users u) {
        Users user =  usersService.checkOne(u);
        if(StringUtils.isNull(user)){
            return new AjaxResult(000001,"查询内容为空，查询失败");
        }else{
            return new AjaxResult(200,"查询成功",user);
        }
    }

}
